package com.ruoyi.kjcg.qygfgb.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.kjcg.qygfgb.service.IQygfgbService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import java.io.InputStream;

import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.kjcg.qygfgb.domain.Qygfgb;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 企业工法公布Controller
 *
 * @author huacai
 * @date 2024-11-05
 */
@RestController
@RequestMapping("/kjcg/qygfgb")
public class QygfgbController extends BaseController {
    @Resource
    private IQygfgbService qygfgbService;

    /**
     * 查询企业工法公布列表
     */
    @PreAuthorize("@ss.hasPermi('kjcg:qygfgb:list')")
    @GetMapping("/list")
    public TableDataInfo list(Qygfgb qygfgb) {
        startPage();
        List<Qygfgb> list = qygfgbService.selectQygfgbList(qygfgb);
        return getDataTable(list);
    }

    /**
     * 导出企业工法公布列表
     */
    @PreAuthorize("@ss.hasPermi('kjcg:qygfgb:export')")
    @Log(title = "企业工法公布", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Qygfgb qygfgb) {
        List<Qygfgb> list = qygfgbService.selectQygfgbList(qygfgb);
        ExcelUtil<Qygfgb> util = new ExcelUtil<Qygfgb>(Qygfgb.class);
        util.exportExcel(response, list, "企业工法公布数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Qygfgb> util = new ExcelUtil<Qygfgb>(Qygfgb.class);
        util.importTemplateExcel(response, "企业工法公布数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "企业工法公布", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('kjcg:qygfgb:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Qygfgb> util = new ExcelUtil<Qygfgb>(Qygfgb.class);
        InputStream inputStream = file.getInputStream();
        List<Qygfgb> list = util.importExcel(inputStream);
        inputStream.close();
        int count = qygfgbService.batchInsertQygfgb(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取企业工法公布详细信息
     */
    @PreAuthorize("@ss.hasPermi('kjcg:qygfgb:query')")
    @GetMapping(value = "/{qygfgbId}")
    public AjaxResult getInfo(@PathVariable("qygfgbId") Integer qygfgbId) {
        return success(qygfgbService.selectQygfgbByQygfgbId(qygfgbId));
    }

    /**
     * 企业工法公布
     */
    @PreAuthorize("@ss.hasPermi('kjcg:qygfgb:add')")
    @Log(title = "企业工法公布", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Qygfgb qygfgb) {
        qygfgb.setUserId(Math.toIntExact(getUserId())); //用户ID
        qygfgb.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        qygfgb.setCreateBy(getUsername()); //创建人
        qygfgb.setIdWhere("存在");
        return toAjax(qygfgbService.insertQygfgb(qygfgb));
    }

    /**
     * 企业工法评审未通过
     */
    @Log(title = "企业工法评审未通过", businessType = BusinessType.INSERT)
    @PostMapping("/insertPsWTg")
    public AjaxResult insertPsWTg(@RequestBody Qygfgb qygfgb) {
        qygfgb.setUserId(Math.toIntExact(getUserId())); //用户ID
        qygfgb.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        qygfgb.setCreateBy(getUsername()); //创建人
        qygfgb.setIdWhere("存在");
        return toAjax(qygfgbService.insertPsWTg(qygfgb));
    }

    /**
     * 修改企业工法公布
     */
    @PreAuthorize("@ss.hasPermi('kjcg:qygfgb:edit')")
    @Log(title = "企业工法公布", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Qygfgb qygfgb) {
        return toAjax(qygfgbService.updateQygfgb(qygfgb));
    }

    /**
     * 删除企业工法公布
     */
    @PreAuthorize("@ss.hasPermi('kjcg:qygfgb:remove')")
    @Log(title = "企业工法公布", businessType = BusinessType.DELETE)
    @DeleteMapping("/{qygfgbIds}")
    public AjaxResult remove(@PathVariable Integer[] qygfgbIds) {
        return toAjax(qygfgbService.deleteQygfgbByQygfgbIds(qygfgbIds));
    }
}
